package com.x.aoki.order.openfeign.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.x.aoki.common.pojo.Payment;
import com.x.aoki.common.utils.CR;
import com.x.aoki.order.openfeign.hystrix.service.PaymentFeignHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author caoy
 * @date 2025-02-17 14:35:26
 */
@RestController
@Slf4j
// 在消费端配置全局的降级处理
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderFeignHystrixController {
    @Resource
    private PaymentFeignHystrixService paymentFeignHystrixService;

    @GetMapping("payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentFeignHystrixService.paymentInfo_OK(id);
    }

    @GetMapping("payment/hystrix/timeout/{id}")
    // 消费端的降级服务配置，原则上项目的降级配置都是在消费端配置
    // @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutFallbackMethod",
    //         commandProperties = {
    //                 @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    //         })
    @HystrixCommand
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        return paymentFeignHystrixService.paymentInfo_Timeout(id);
    }

    // 这个降级方法的参数要和配置降级的方法的参数列表一致，否则会报错
    public String paymentInfo_TimeoutFallbackMethod(Integer id){
        return "这是消费者 80，对方支付系统繁忙，请 10 秒钟之后再试或检查自己的错误。"+id;
    }

    // 全局的降级处理方法
    public String payment_Global_FallbackMethod(){
        return "Global异常的处理信息，请稍后再试。。。。。。。";
    }

    @GetMapping("payment/hystrix/circuitbreaker/timeout/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        return paymentFeignHystrixService.paymentCircuitBreaker(id);
    }
}
